package com.crm.service.system.department.controller;

import com.crm.model.common.dtos.ResponseResult;
import com.crm.model.system.department.dtos.DepartmentDto;
import com.crm.model.system.department.dtos.DepartmentPageDto;
import com.crm.service.system.department.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/department")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    /**
     * 部门分页查询
     *
     * @param page
     * @param pageSize
     * @param departmentName
     * @param status
     * @return
     */
    @GetMapping("/page")
    public ResponseResult pageList(@RequestParam Integer page,
                                   @RequestParam Integer pageSize,
                                   @RequestParam(required = false) String departmentName,
                                   @RequestParam(required = false) Integer status) {
        DepartmentPageDto departmentPageDto = DepartmentPageDto.builder()
                .page(page)
                .pageSize(pageSize)
                .departmentName(departmentName)
                .status(status)
                .build();
        return departmentService.pageList(departmentPageDto);
    }

    @GetMapping("/list")
    public ResponseResult departmentNameList() {
        return departmentService.namelist();
    }

    /**
     * 根据ID查询部门
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseResult selectDepartmentById(@PathVariable Integer id) {
        return departmentService.selectDepartmentById(id);
    }

    /**
     * 添加部门
     *
     * @param departmentDto
     * @return
     */
    @PostMapping()
    public ResponseResult saveDepartment(@RequestBody DepartmentDto departmentDto) {
        return departmentService.saveDepartment(departmentDto);
    }

    /**
     * 修改部门
     *
     * @param departmentDto
     * @return
     */
    @PutMapping()
    public ResponseResult updateDepartment(@RequestBody DepartmentDto departmentDto) {
        return departmentService.updateDepartment(departmentDto);
    }

    @DeleteMapping()
    public ResponseResult deleteDepartment(@RequestParam Integer id) {
        return departmentService.deleteDepartment(id);
    }
}
